package com.harmontronics.app.pedestal.bridge;

import android.database.Cursor;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.blankj.utilcode.util.LogUtils;
import com.harmontronics.app.pedestal.bridge.model.BaseBridge;
import com.harmontronics.app.pedestal.db.CustomDBManager;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wendu.dsbridge.CompletionHandler;

public class SqliteBridge extends BaseBridge {

    @JavascriptInterface
    public void execute(Object argsJson, CompletionHandler<String> handler) {
        LogUtils.d("SQLITE.execute");
        if (null == argsJson) {
            handler.complete(fail("参数不能为空", null));
            return;
        }

        try {
            JSONObject args = (JSONObject) argsJson;
            LogUtils.d("args: " + args.toString());
            String database = args.getString("database");
            String sql = args.getString("sql");
            if (TextUtils.isEmpty(database)) {
                throw new IllegalArgumentException("数据库名称不能为空");
            }
            if (TextUtils.isEmpty(sql)) {
                throw new IllegalArgumentException("SQL语句不能为空");
            }
            CustomDBManager.getInstance().execSQL(database, sql);
            handler.complete(success("execute success"));
        } catch (Exception e) {
            handler.complete(fail(e.getMessage(), e));
        }
    }

    @JavascriptInterface
    public void query(Object argsJson, CompletionHandler<String> handler) {
        LogUtils.d("SQLITE.query");
        if (null == argsJson) {
            handler.complete(fail("参数不能为空", null));
            return;
        }

        try {
            JSONObject args = (JSONObject) argsJson;
            LogUtils.d("args: " + args.toString());
            String database = args.getString("database");
            String sql = args.getString("sql");
            JSONArray results = args.getJSONArray("results");
            if (TextUtils.isEmpty(database)) {
                throw new IllegalArgumentException("数据库名称不能为空");
            }
            if (TextUtils.isEmpty(sql)) {
                throw new IllegalArgumentException("SQL语句不能为空");
            }
            if (results.length() == 0) {
                throw new IllegalArgumentException("结果集(results)不能为空");
            }
            Cursor cursor = CustomDBManager.getInstance().execQuery(database, sql, null);
            if (null == cursor) {
                handler.complete(success(null));
            } else {
                List list = new ArrayList<>();
                while (cursor.moveToNext()) {
                    Map<String, Object> item = new HashMap<>();

                    for (int i = 0; i < results.length(); i++) {
                        String key = results.getJSONObject(i).getString("key");
                        String type = results.getJSONObject(i).getString("type");

                        Object value = null;
                        int keyIndex = cursor.getColumnIndex(key);
                        switch (type) {
                            case "string":
                                value = cursor.getString(keyIndex);
                                break;
                            case "short":
                                value = cursor.getShort(keyIndex);
                                break;
                            case "int":
                                value = cursor.getInt(keyIndex);
                                break;
                            case "float":
                                value = cursor.getFloat(keyIndex);
                                break;
                            case "double":
                                value = cursor.getDouble(keyIndex);
                                break;
                            case "long":
                                value = cursor.getLong(keyIndex);
                                break;
                            case "blob":
                                value = cursor.getBlob(keyIndex);
                                break;
                            default:
                                break;
                        }

                        item.put(key, value);
                    }
                    list.add(item);
                }
                handler.complete(success(list));
            }
        } catch (Exception e) {
            handler.complete(fail(e.getMessage(), e));
        }
    }

}
